package com.ui;

import com.model.Group;
import com.service.GroupService;
import com.service.StudentService;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Random;

public class RandomGroupDialog {
    private JDialog dialog;
    private JButton selectGroupButton;
    private JLabel groupResultLabel;
    private StudentService studentService;
    private String currentClass;
    private GroupService groupService;
    private List<Group> groups;

    public RandomGroupDialog(StudentService studentService, GroupService groupService, String currentClass, List<Group> groups) {
        this.studentService = studentService;

        this.groupService = groupService;
        this.currentClass = currentClass;
        this.groups = groups; // 确保 groups 被正确赋值


        System.out.println("group: " + groups);

        selectGroupButton = new JButton("随机挑选一个小组");
        groupResultLabel = new JLabel("小组结果将在这里显示");

        setLayout();

        selectGroupButton.addActionListener(e -> {
            String randomGroup = getRandomGroupFromDatabase();
            groupResultLabel.setText("随机挑选的小组: " + randomGroup);
        });
    }

    private void setLayout() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 1));
        panel.add(selectGroupButton);
        panel.add(groupResultLabel);
        
        dialog = new JDialog();
        dialog.setTitle("随机小组选择器");
        dialog.setContentPane(panel);
        dialog.setSize(300, 150);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setLocationRelativeTo(null);
    }

    private String getRandomGroupFromDatabase() {

        if (!this.groups.isEmpty()) {
            Random random = new Random();
            return String.valueOf(this.groups.get(random.nextInt(this.groups.size())));
        } else {
            return "没有找到小组";
        }
    }

    public void setVisible(boolean visible) {
        dialog.setVisible(visible);
    }
}
